package org.htmlparser.beans;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.PrintStream;
import java.io.Serializable;
import java.net.URLConnection;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.Translate;
import org.htmlparser.visitors.NodeVisitor;

public class StringBean extends NodeVisitor
  implements Serializable
{
  private static final String NEWLINE = System.getProperty("line.separator");
  private static final int NEWLINE_SIZE = 0;
  public static final String PROP_COLLAPSE_PROPERTY = "collapse";
  public static final String PROP_CONNECTION_PROPERTY = "connection";
  public static final String PROP_LINKS_PROPERTY = "links";
  public static final String PROP_REPLACE_SPACE_PROPERTY = "replaceNonBreakingSpaces";
  public static final String PROP_STRINGS_PROPERTY = "strings";
  public static final String PROP_URL_PROPERTY = "URL";
  protected StringBuffer mBuffer;
  protected boolean mCollapse;
  protected int mCollapseState;
  protected boolean mIsPre;
  protected boolean mIsScript;
  protected boolean mIsStyle;
  protected boolean mLinks;
  protected Parser mParser;
  protected PropertyChangeSupport mPropertySupport;
  protected boolean mReplaceSpace;
  protected String mStrings;

  public StringBean()
  {
    super(true, true);
    this.mPropertySupport = new PropertyChangeSupport(this);
    this.mParser = new Parser();
    this.mStrings = null;
    this.mLinks = false;
    this.mReplaceSpace = true;
    this.mCollapse = true;
    this.mCollapseState = 0;
    this.mBuffer = new StringBuffer(4096);
    this.mIsScript = false;
    this.mIsPre = false;
    this.mIsStyle = false;
  }

  public static void main(String[] paramArrayOfString)
  {
    if (paramArrayOfString.length <= 0)
      System.out.println("Usage: java -classpath htmlparser.jar org.htmlparser.beans.StringBean <http://whatever_url>");
    while (true)
    {
      return;
      StringBean localStringBean = new StringBean();
      localStringBean.setLinks(false);
      localStringBean.setReplaceNonBreakingSpaces(true);
      localStringBean.setCollapse(true);
      localStringBean.setURL(paramArrayOfString[0]);
      System.out.println(localStringBean.getStrings());
    }
  }

  private void resetStrings()
  {
    if (this.mStrings != null);
    try
    {
      this.mParser.setURL(getURL());
      setStrings();
      return;
    }
    catch (ParserException localParserException)
    {
      updateStrings(localParserException.toString());
    }
  }

  public void addPropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    this.mPropertySupport.addPropertyChangeListener(paramPropertyChangeListener);
  }

  protected void carriageReturn()
  {
    int i = this.mBuffer.length();
    if ((i != 0) && (NEWLINE_SIZE <= i) && (!(this.mBuffer.substring(i - NEWLINE_SIZE, i).equals(NEWLINE))))
      this.mBuffer.append(NEWLINE);
    this.mCollapseState = 0;
  }

  protected void collapse(StringBuffer paramStringBuffer, String paramString)
  {
    int i = paramString.length();
    if (i != 0)
    {
      int j = 0;
      if (j < i)
      {
        char c = paramString.charAt(j);
        switch (c)
        {
        default:
          if (1 == this.mCollapseState)
            paramStringBuffer.append(' ');
          this.mCollapseState = 2;
          paramStringBuffer.append(c);
        case '\t':
        case '\n':
        case '\f':
        case '\r':
        case ' ':
        case '​':
        }
        while (true)
        {
          do
            while (true)
              ++j;
          while (this.mCollapseState == 0);
          this.mCollapseState = 1;
        }
      }
    }
  }

  protected String extractStrings()
    throws ParserException
  {
    this.mCollapseState = 0;
    this.mParser.visitAllNodesWith(this);
    String str = this.mBuffer.toString();
    this.mBuffer = new StringBuffer(4096);
    return str;
  }

  public boolean getCollapse()
  {
    return this.mCollapse;
  }

  public URLConnection getConnection()
  {
    URLConnection localURLConnection;
    if (this.mParser != null)
      localURLConnection = this.mParser.getConnection();
    while (true)
    {
      return localURLConnection;
      localURLConnection = null;
    }
  }

  public boolean getLinks()
  {
    return this.mLinks;
  }

  public boolean getReplaceNonBreakingSpaces()
  {
    return this.mReplaceSpace;
  }

  public String getStrings()
  {
    if (this.mStrings == null)
    {
      if (this.mBuffer.length() != 0)
        break label26;
      setStrings();
    }
    while (true)
    {
      return this.mStrings;
      label26: updateStrings(this.mBuffer.toString());
    }
  }

  public String getURL()
  {
    String str;
    if (this.mParser != null)
      str = this.mParser.getURL();
    while (true)
    {
      return str;
      str = null;
    }
  }

  public void removePropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    this.mPropertySupport.removePropertyChangeListener(paramPropertyChangeListener);
  }

  public void setCollapse(boolean paramBoolean)
  {
    this.mCollapseState = 0;
    boolean bool = this.mCollapse;
    if (bool != paramBoolean)
    {
      this.mCollapse = paramBoolean;
      this.mPropertySupport.firePropertyChange("collapse", bool, paramBoolean);
      resetStrings();
    }
  }

  public void setConnection(URLConnection paramURLConnection)
  {
    String str = getURL();
    URLConnection localURLConnection = getConnection();
    if (((localURLConnection == null) && (paramURLConnection != null)) || ((localURLConnection != null) && (!(localURLConnection.equals(paramURLConnection)))))
      try
      {
        if (this.mParser == null)
        {
          this.mParser = new Parser(paramURLConnection);
          this.mPropertySupport.firePropertyChange("URL", str, getURL());
          this.mPropertySupport.firePropertyChange("connection", localURLConnection, this.mParser.getConnection());
          setStrings();
        }
        else
        {
          this.mParser.setConnection(paramURLConnection);
        }
      }
      catch (ParserException localParserException)
      {
        updateStrings(localParserException.toString());
      }
  }

  public void setLinks(boolean paramBoolean)
  {
    boolean bool = this.mLinks;
    if (bool != paramBoolean)
    {
      this.mLinks = paramBoolean;
      this.mPropertySupport.firePropertyChange("links", bool, paramBoolean);
      resetStrings();
    }
  }

  public void setReplaceNonBreakingSpaces(boolean paramBoolean)
  {
    boolean bool = this.mReplaceSpace;
    if (bool != paramBoolean)
    {
      this.mReplaceSpace = paramBoolean;
      this.mPropertySupport.firePropertyChange("replaceNonBreakingSpaces", bool, paramBoolean);
      resetStrings();
    }
  }

  // ERROR //
  protected void setStrings()
  {
    // Byte code:
    //   0: aload_0
    //   1: iconst_0
    //   2: putfield 91	org/htmlparser/beans/StringBean:mCollapseState	I
    //   5: aload_0
    //   6: invokevirtual 142	org/htmlparser/beans/StringBean:getURL	()Ljava/lang/String;
    //   9: ifnull +193 -> 202
    //   12: aload_0
    //   13: getfield 81	org/htmlparser/beans/StringBean:mParser	Lorg/htmlparser/Parser;
    //   16: aload_0
    //   17: invokevirtual 183	org/htmlparser/Parser:visitAllNodesWith	(Lorg/htmlparser/visitors/NodeVisitor;)V
    //   20: aload_0
    //   21: aload_0
    //   22: getfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   25: invokevirtual 184	java/lang/StringBuffer:toString	()Ljava/lang/String;
    //   28: invokevirtual 152	org/htmlparser/beans/StringBean:updateStrings	(Ljava/lang/String;)V
    //   31: aload_0
    //   32: new 93	java/lang/StringBuffer
    //   35: dup
    //   36: sipush 4096
    //   39: invokespecial 96	java/lang/StringBuffer:<init>	(I)V
    //   42: putfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   45: goto +176 -> 221
    //   48: astore_1
    //   49: aload_0
    //   50: new 93	java/lang/StringBuffer
    //   53: dup
    //   54: sipush 4096
    //   57: invokespecial 96	java/lang/StringBuffer:<init>	(I)V
    //   60: putfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   63: aload_1
    //   64: athrow
    //   65: astore_3
    //   66: aload_0
    //   67: iconst_0
    //   68: putfield 102	org/htmlparser/beans/StringBean:mIsPre	Z
    //   71: aload_0
    //   72: iconst_0
    //   73: putfield 100	org/htmlparser/beans/StringBean:mIsScript	Z
    //   76: aload_0
    //   77: iconst_0
    //   78: putfield 104	org/htmlparser/beans/StringBean:mIsStyle	Z
    //   81: aload_0
    //   82: getfield 81	org/htmlparser/beans/StringBean:mParser	Lorg/htmlparser/Parser;
    //   85: invokevirtual 220	org/htmlparser/Parser:reset	()V
    //   88: aload_0
    //   89: new 93	java/lang/StringBuffer
    //   92: dup
    //   93: sipush 4096
    //   96: invokespecial 96	java/lang/StringBuffer:<init>	(I)V
    //   99: putfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   102: aload_0
    //   103: iconst_0
    //   104: putfield 91	org/htmlparser/beans/StringBean:mCollapseState	I
    //   107: aload_0
    //   108: getfield 81	org/htmlparser/beans/StringBean:mParser	Lorg/htmlparser/Parser;
    //   111: aload_0
    //   112: invokevirtual 183	org/htmlparser/Parser:visitAllNodesWith	(Lorg/htmlparser/visitors/NodeVisitor;)V
    //   115: aload_0
    //   116: aload_0
    //   117: getfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   120: invokevirtual 184	java/lang/StringBuffer:toString	()Ljava/lang/String;
    //   123: invokevirtual 152	org/htmlparser/beans/StringBean:updateStrings	(Ljava/lang/String;)V
    //   126: aload_0
    //   127: new 93	java/lang/StringBuffer
    //   130: dup
    //   131: sipush 4096
    //   134: invokespecial 96	java/lang/StringBuffer:<init>	(I)V
    //   137: putfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   140: goto +81 -> 221
    //   143: astore 5
    //   145: aload_0
    //   146: aload 5
    //   148: invokevirtual 149	ParserException:toString	()Ljava/lang/String;
    //   151: invokevirtual 152	org/htmlparser/beans/StringBean:updateStrings	(Ljava/lang/String;)V
    //   154: aload_0
    //   155: new 93	java/lang/StringBuffer
    //   158: dup
    //   159: sipush 4096
    //   162: invokespecial 96	java/lang/StringBuffer:<init>	(I)V
    //   165: putfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   168: goto +53 -> 221
    //   171: astore 4
    //   173: aload_0
    //   174: new 93	java/lang/StringBuffer
    //   177: dup
    //   178: sipush 4096
    //   181: invokespecial 96	java/lang/StringBuffer:<init>	(I)V
    //   184: putfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   187: aload 4
    //   189: athrow
    //   190: astore_2
    //   191: aload_0
    //   192: aload_2
    //   193: invokevirtual 149	ParserException:toString	()Ljava/lang/String;
    //   196: invokevirtual 152	org/htmlparser/beans/StringBean:updateStrings	(Ljava/lang/String;)V
    //   199: goto +22 -> 221
    //   202: aload_0
    //   203: aconst_null
    //   204: putfield 83	org/htmlparser/beans/StringBean:mStrings	Ljava/lang/String;
    //   207: aload_0
    //   208: new 93	java/lang/StringBuffer
    //   211: dup
    //   212: sipush 4096
    //   215: invokespecial 96	java/lang/StringBuffer:<init>	(I)V
    //   218: putfield 98	org/htmlparser/beans/StringBean:mBuffer	Ljava/lang/StringBuffer;
    //   221: return
    //
    // Exception table:
    //   from	to	target	type
    //   12	31	48	finally
    //   31	65	65	org/htmlparser/util/EncodingChangeException
    //   81	126	143	ParserException
    //   81	126	171	finally
    //   145	154	171	finally
    //   31	65	190	ParserException
  }

  public void setURL(String paramString)
  {
    String str = getURL();
    URLConnection localURLConnection = getConnection();
    if (((str == null) && (paramString != null)) || ((str != null) && (!(str.equals(paramString)))))
      try
      {
        if (this.mParser == null)
        {
          this.mParser = new Parser(paramString);
          this.mPropertySupport.firePropertyChange("URL", str, getURL());
          this.mPropertySupport.firePropertyChange("connection", localURLConnection, this.mParser.getConnection());
          setStrings();
        }
        else
        {
          this.mParser.setURL(paramString);
        }
      }
      catch (ParserException localParserException)
      {
        updateStrings(localParserException.toString());
      }
  }

  protected void updateStrings(String paramString)
  {
    if ((this.mStrings == null) || (!(this.mStrings.equals(paramString))))
    {
      String str = this.mStrings;
      this.mStrings = paramString;
      this.mPropertySupport.firePropertyChange("strings", str, paramString);
    }
  }

  public void visitEndTag(Tag paramTag)
  {
    String str = paramTag.getTagName();
    if (str.equalsIgnoreCase("PRE"))
      this.mIsPre = false;
    while (true)
    {
      do
        while (true)
        {
          return;
          if (!(str.equalsIgnoreCase("SCRIPT")))
            break;
          this.mIsScript = false;
        }
      while (!(str.equalsIgnoreCase("STYLE")));
      this.mIsStyle = false;
    }
  }

  public void visitStringNode(Text paramText)
  {
    String str1;
    String str2;
    if ((!(this.mIsScript)) && (!(this.mIsStyle)))
    {
      str1 = paramText.getText();
      if (this.mIsPre)
        break label84;
      str2 = Translate.decode(str1);
      if (getReplaceNonBreakingSpaces())
        str2 = str2.replace(160, ' ');
      if (!(getCollapse()))
        break label71;
      collapse(this.mBuffer, str2);
    }
    while (true)
    {
      while (true)
      {
        return;
        label71: this.mBuffer.append(str2);
      }
      label84: this.mBuffer.append(str1);
    }
  }

  public void visitTag(Tag paramTag)
  {
    if ((paramTag instanceof LinkTag) && (getLinks()))
    {
      this.mBuffer.append("<");
      this.mBuffer.append(((LinkTag)paramTag).getLink());
      this.mBuffer.append(">");
    }
    String str = paramTag.getTagName();
    if (str.equalsIgnoreCase("PRE"))
      this.mIsPre = true;
    while (true)
    {
      do
        while (true)
        {
          if (paramTag.breaksFlow())
            carriageReturn();
          return;
          if (!(str.equalsIgnoreCase("SCRIPT")))
            break;
          this.mIsScript = true;
        }
      while (!(str.equalsIgnoreCase("STYLE")));
      this.mIsStyle = true;
    }
  }
}